package com.lengxf.mybatis.generate;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ColumnType {

    DATE("datetime", "DATETIME", null),
    VARCHAR("varchar(64)", "STRING", "''"),
    INT("int", "INTEGER", 0),
    BIGINT("bigint", "", 0),
    DECIMAL("decimal(10,2)", "DECIMAL", 0),
    FLOAT("decimal(10,2)", "FLOAT", 0),
    CHAR("char(1)", "BOOLEAN", 0),
    DEFAULT("varchar(64)", "DEFAULT", "''");

    private final String type;

    private final String originType;

    private final Object defaultValue;

    public static ColumnType getType(String columnName, String type) {
        String processColumnName = processColumnName(columnName);
        if (processColumnName.endsWith("id") ||
                processColumnName.startsWith("id") ||
                processColumnName.endsWith("key")) {
            return BIGINT;
        }
        if (columnName.toLowerCase().contains("status")) {
            return CHAR;
        }
        for (ColumnType columnType : ColumnType.values()) {
            if (columnType.getOriginType().equalsIgnoreCase(type)) {
                return columnType;
            }
        }
        return DEFAULT;
    }

    private static String processColumnName(String columnName) {
        if (columnName.endsWith("_S") || columnName.endsWith("_I")
                || columnName.endsWith("_B") || columnName.endsWith("F")
                || columnName.endsWith("_T") || columnName.equals("_Y")) {
            int lasted = columnName.lastIndexOf("_");
            return columnName.substring(0, lasted).toLowerCase();
        }
        return columnName.toLowerCase();
    }

}
